**Open Quality of Government Environmental Indicators dataset
use "/Users/carterahanson/Downloads/Capstone Stata/qog_ei_sept21.dta"

*Get rid of unused variables
keep cname ccode year cname_qog ccode_qog ccodealp ccodealp_year ccodecow ccodevdem cname_year act_act bti_envc ccl_exepp ccl_leglp ccl_lpp ccl_mitlpp edgar_co2pc edgar_co2t emdat_damage emdat_naffect emdat_ndeath emdat_ntotaff engo_nengo ef_carb gg_etp iead_eif1 iead_eif2 iead_eif3 iead_rat iead_sig iead_term wdi_co2 wdi_fossil wvs_ameop wvs_ceom

*Fix some of the problems with the dataset
sort cname year
replace ccodecow=625 if cname=="Sudan"
replace ccodecow=530 if cname=="Ethiopia"
replace ccodecow=345 if cname=="Serbia and Montenegro"
replace ccodecow=345 if cname=="Serbia"
drop if cname=="Serbia" &year<1992
drop if cname=="Serbia and Montenegro" &year<1992
drop if cname=="Yugoslavia" &year>1991
drop if cname=="Serbia" &year<2006
drop if cname=="Serbia and Montenegro" &year>2005
replace ccodecow=678 if ccode_qog==886
replace ccodecow=680 if ccode_qog==720
replace ccodecow=817 if cname=="South Vietnam" 

drop if cname=="German Democratic Republic" &year>1990
drop if cname=="Czechoslovakia" &year>1992
drop if cname=="Czech Republic" &year<1993
drop if cname=="Slovakia" &year<1993
drop if cname=="Montenegro" &year<2006
drop if cname=="North Macedonia" &year<1993
drop if cname=="Croatia" &year<1992
drop if cname=="Bosnia and Herzegovina" &year<1992
drop if cname=="Slovenia" &year<1991
drop if cname=="Republic of Moldova" &year<1992

sort ccodecow cname year
replace ccodecow=352 if cname=="Cyprus"
replace ccodecow=220 if cname=="France"
replace ccodecow=260 if cname_qog=="Germany, West"
replace cname = "German Federal Republic" if cname_qog=="Germany, West"
drop if cname=="Tibet"
drop if cname=="North Vietnam" &year<1955
drop if cname=="South Vietnam" &year<1955
drop if cname=="North Vietnam" &year>1976
drop if cname=="South Vietnam" &year>1976
replace cname = "Vietnam" if cname=="Viet Nam"
drop if cname=="Vietnam" &year<1977

replace ccodecow=820 if cname=="Malaysia"
replace ccodecow=770 if cname=="Pakistan"
drop if cname=="USSR" &year>1991
drop if cname=="Russia" &year<1992
replace ccodecow=365 if cname=="USSR"

save "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/qog_ei_sept21.1DF.dta"

**Open Quality of Government Standard dataset
use "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/qog_std_ts_jan23_stata14.dta"

*Get rid of unused variables
keep ccode cname year ccode_qog cname_qog ccodealp ccodecow cname_year ccodealp_year ht_ipolity2 p_polity2 gpi_gpi bti_csp bti_pp ciri_assn fh_pr yri_agi30 yri_meanage yri_medianage yri_mp30 vdem_partipdem vdem_polyarchy vdem_delibdem

*Fix some of the problems with the dataset
sort cname year
replace ccodecow=625 if cname=="Sudan"
replace ccodecow=530 if cname=="Ethiopia"
replace ccodecow=345 if cname=="Serbia and Montenegro"
replace ccodecow=345 if cname=="Serbia"
drop if cname=="Serbia" &year<1992
drop if cname=="Serbia and Montenegro" &year<1992
drop if cname=="Yugoslavia" &year>1991
drop if cname=="Serbia" &year<2006
drop if cname=="Serbia and Montenegro" &year>2005
replace ccodecow=678 if ccode_qog==886
replace ccodecow=680 if ccode_qog==720
replace ccodecow=817 if cname=="South Vietnam" 

drop if cname=="Czechia" &year<1993
drop if cname=="Czechoslovakia" &year>1992
drop if cname=="Slovakia" &year<1993
drop if cname=="Montenegro" &year<2006
drop if cname=="North Macedonia" &year<1993
drop if cname=="Croatia" &year<1992
drop if cname=="Bosnia and Herzegovina" &year<1992
drop if cname=="Slovenia" &year<1991
drop if cname=="Moldova (the Republic of)" &year<1992

sort ccodecow cname year
replace ccodecow=352 if cname=="Cyprus"
replace ccodecow=220 if cname=="France"
replace ccodecow=260 if cname_qog=="Germany, West"
replace cname = "German Federal Republic" if cname_qog=="Germany, West"
drop if cname=="Tibet"
drop if cname=="Vietnam, North" &year<1955
drop if cname=="Vietnam, South" &year<1955
replace cname = "South Vietnam" if cname=="Vietnam, South"
replace cname = "North Vietnam" if cname=="Vietnam, North"
replace ccodecow=817 if cname=="South Vietnam"

replace ccodecow=820 if cname=="Malaysia"
replace ccodecow=770 if cname=="Pakistan"
drop if cname=="USSR" &year>1991
drop if cname=="Russian Federation (the)" &year<1992
replace ccodecow=365 if cname=="USSR"
replace cname = "Russia" if cname=="Russian Federation (the)"


save "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/qog_std_ts_jan23_stata14.2.dta"

**Merge the QoG-EI and QoG Standard datasets
 use "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/qog_ei_sept21.1DF.dta"
merge 1:1 ccodecow year using "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/qog_std_ts_jan23_stata14.2.dta"

list cname ccodecow year if _merge==1
drop if cname=="Democratic Yemen"

*no merging problems, so drop _merge
drop _merge

save "/Users/carterahanson/Downloads/qog_combined_EI:Standard.dta"

**MERGE OF QOG DATASETS COMPLETE**

**Open Varieties of Party Identity and Organization (V-Party) dataset
use "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/V-Dem-CPD-Party-V2.dta"

keep v2paenname v2pashname v2paid pf_party_id country_name histname country_id country_text_id year COWcode v2xpa_antiplural v2xpa_popul v2paseatshare v2panumbseat v2patotalseat v2pavote v2pagovsup v2paanteli v2paplur v2paviol v2paculsup v2pariglef v2pasalie_12 ep_members_vs_leadership ep_people_vs_elite ep_galtan ep_v6_lib_cons

order country_name country_id COWcode year, first
rename COWcode ccodecow
rename country_name cname
rename country_id ccodevdem

sort v2pagovsup ccodecow year
*v2pagovsup is government support, with 0 being senior partner in gov and 1 being junior partner.
drop if v2pagovsup>1
sort ccodecow year
drop v2pashname v2paid pf_party_id histname v2paenname

**Averaging ruling coalition data to produce country-year dataset
*Average values in ruling coalition:
collapse (mean) ave_gov_enviro=v2pasalie_12 (mean) ave_gov_antiplural=v2xpa_antiplural (mean) ave_gov_popul=v2xpa_popul (sum) gov_coalit_seatshare=v2paseatshare (mean) ave_gov_viol=v2paviol (mean) ave_gov_riglef=v2pariglef, by(cname year ccodevdem country_text_id ccodecow)

save "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/V-Dem-CPD-Party-V2.mean.dta"

*Maximum values in ruling coalition:
use "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/V-Dem-CPD-Party-V2.dta"

collapse (max) max_gov_enviro=v2pasalie_12 (max) max_gov_antiplural=v2xpa_antiplural (max) max_gov_popul=v2xpa_popul (sum) gov_coalit_seatshare=v2paseatshare (max) max_gov_viol=v2paviol (max) max_gov_riglef=v2pariglef, by(cname year ccodevdem country_text_id ccodecow)

save "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/V-Dem-CPD-Party-V2.max.dta"

**Combining V-Dem-CPD-Party-V2.mean.dta and V-Dem-CPD-Party-V2.max.dta

merge 1:1 ccodecow year using "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/V-Dem-CPD-Party-V2.mean.dta"

*no merging problems, so drop _merge
drop _merge

save "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/V-Dem-CPD-Party-V2.final.dta"

**MERGE OF VDEM DATASETS COMPLETE**

**Merging V-Dem-CPD-Party-V2.final.dta with qog_combined_EI:Standard.dta
use "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/V-Dem-CPD-Party-V2.final.dta"

drop if cname=="Kosovo"
drop if cname=="Hong Kong"
drop if cname=="Montenegro" &year<2006
drop if cname=="Palestine/West Bank"
drop if cname=="Slovenia" &year<1991
drop if cname=="Somaliland"
drop if cname=="Vietnam" &year<1977
drop if cname=="Zanzibar"

save "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/V-Dem-CPD-Party-V2.final.dta", replace

merge m:m ccodecow year using "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/qog_combined_EI:Standard.dta"

save "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/Merged_VDEM_QOG_Dataset.dta"

**MERGE OF VDEM AND QOG DATASETS COMPLETE**

drop country_text_id

**Drawing down variable above for all blanks for a variable 
bysort ccodecow (year) : replace max_gov_enviro = max_gov_enviro[_n-1] if missing(max_gov_enviro)
bysort ccodecow (year) : replace ave_gov_antiplural = ave_gov_antiplural[_n-1] if missing(ave_gov_antiplural)
bysort ccodecow (year) : replace ave_gov_popul = ave_gov_popul[_n-1] if missing(ave_gov_popul)
bysort ccodecow (year) : replace gov_coalit_seatshare = gov_coalit_seatshare[_n-1] if missing(gov_coalit_seatshare)
bysort ccodecow (year) : replace ave_gov_viol = ave_gov_viol[_n-1] if missing(ave_gov_viol)
bysort ccodecow (year) : replace ave_gov_riglef = ave_gov_riglef[_n-1] if missing(ave_gov_riglef)
bysort ccodecow (year) : replace ave_gov_enviro = ave_gov_enviro[_n-1] if missing(ave_gov_enviro)
bysort ccodecow (year) : replace max_gov_antiplural = max_gov_antiplural[_n-1] if missing(max_gov_antiplural)
bysort ccodecow (year) : replace max_gov_popul = max_gov_popul[_n-1] if missing(max_gov_popul)
bysort ccodecow (year) : replace max_gov_viol = max_gov_viol[_n-1] if missing(max_gov_viol)
bysort ccodecow (year) : replace max_gov_riglef = max_gov_riglef[_n-1] if missing(max_gov_riglef)

order cname year ccodecow cname_qog ccodevdem ccode ccode_qog ccodealp ccodealp_year cname_year
order cname_qog ccode ccode_qog ccodealp ccodealp_year cname_year _merge, last
order ave_gov_enviro, after (ccodevdem)

replace cname = "Bolivia" if cname=="Bolivia (Plurinational State of)"
replace cname = "United Kingdom" if cname=="United Kingdom of Great Britain and Northern Ireland"
replace cname = "German Federal Republic" if ccodecow==260
replace cname = "Czechoslovakia" if ccodecow==315
replace cname = "Yugoslavia" if ccodecow==345 &year<1992
replace cname = "Serbia and Montenegro" if ccodecow==345 &year>1991 &year<2006
replace cname = "Moldova" if cname=="Republic of Moldova"
replace cname = "USSR" if cname=="Russia" &year<1992
replace cname = "Cabo Verde" if cname=="Cape Verde"
replace cname = "Guinea Bissau" if cname=="Guinea-Bissau"
replace cname = "The Gambia" if cname=="Gambia (Republic of The)"
replace cname = "Côte D'Ivoire" if cname=="Ivory Coast"
replace cname = "Republic of the Congo" if cname=="Congo"
replace cname = "Tanzania" if cname=="United Republic of Tanzania"
replace cname = "Iran" if cname=="Iran (Islamic Republic of)"
replace cname = "Syria" if cname=="Syrian Arab Republic"
replace cname = "Democratic People's Republic of Korea" if cname=="North Korea"
replace cname = "Republic of Korea" if cname=="South Korea"
replace cname = "Myanmar" if cname=="Burma/Myanmar"
replace cname = "Lao People's Democratic Republic" if cname=="Laos"
replace cname = "Venezuela" if cname=="Venezuela, Bolivarian Republic of"

save "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/Merged_VDEM_QOG_Dataset.dta", replace

**MERGED DATASET COMPLETE**

*Make variable that is total of IEAs entered into force by country by year
egen total = rowtotal(iead_eif1 iead_eif2 iead_eif3)
rename total iead_sum

save "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/Merged_VDEM_QOG_Dataset.dta", replace

**Export STATA file to an excel sheet. 
	*Drop "gg_etp" variable
	*Add new variable "emdat_damage_decay" which represents the total $ amount spent on natural disaster recovery ("emdat_damage") decayed over a 3 year period, such that emdat_damage_decay for year X = emdat_damage(X) + (.5 * emdat_damage(X-1)) + (.25 * emdat_damage(X-2)). For all years where there is no data for emdat_damage, emdat_damage_decay was also left blank.
	*Add new variable "iead_eif1_5year" which is the total of iead_eif1 across a 5 year period. For example, iead_eif1_5year for the year 2010 is the sum of iead_eif1 for 2008, 2009, 2010, 2011, and 2012. The first and last 2 years of data for each country were left blank for iead_eif1_5year to ensure that each datapoint includes 5 years. Variables were made in the same way corresponding to iead_eif2, iead_eif3, iead_rat, iead_sig,  iead_sum, and iead_term with "_5year" added to the end of each new variable.
	
**Import excel spreadsheet as new STATA file
save "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/WithDecay&5yrs.dta"

**Open Quality of Government Standard dataset
use "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/qog_std_ts_jan23_stata14.dta"

*Fix some of the problems with the dataset
sort cname year
replace ccodecow=625 if cname=="Sudan"
replace ccodecow=530 if cname=="Ethiopia"
replace ccodecow=345 if cname=="Serbia and Montenegro"
replace ccodecow=345 if cname=="Serbia"
drop if cname=="Serbia" &year<1992
drop if cname=="Serbia and Montenegro" &year<1992
drop if cname=="Yugoslavia" &year>1991
drop if cname=="Serbia" &year<2006
drop if cname=="Serbia and Montenegro" &year>2005
replace ccodecow=678 if ccode_qog==886
replace ccodecow=680 if ccode_qog==720
replace ccodecow=817 if cname=="South Vietnam" 

drop if cname=="Czechia" &year<1993
drop if cname=="Czechoslovakia" &year>1992
drop if cname=="Slovakia" &year<1993
drop if cname=="Montenegro" &year<2006
drop if cname=="North Macedonia" &year<1993
drop if cname=="Croatia" &year<1992
drop if cname=="Bosnia and Herzegovina" &year<1992
drop if cname=="Slovenia" &year<1991
drop if cname=="Moldova (the Republic of)" &year<1992

sort ccodecow cname year
replace ccodecow=352 if cname=="Cyprus"
replace ccodecow=220 if cname=="France"
replace ccodecow=260 if cname_qog=="Germany, West"
replace cname = "German Federal Republic" if cname_qog=="Germany, West"
drop if cname=="Tibet"
drop if cname=="Vietnam, North" &year<1955
drop if cname=="Vietnam, South" &year<1955
replace cname = "South Vietnam" if cname=="Vietnam, South"
replace cname = "North Vietnam" if cname=="Vietnam, North"
replace ccodecow=817 if cname=="South Vietnam"

replace ccodecow=820 if cname=="Malaysia"
replace ccodecow=770 if cname=="Pakistan"
drop if cname=="USSR" &year>1991
drop if cname=="Russian Federation (the)" &year<1992
replace ccodecow=365 if cname=="USSR"
replace cname = "Russia" if cname=="Russian Federation (the)"

order cname year ccodecow cname_qog  ccode ccode_qog ccodealp ccodealp_year cname_year
sort ccodecow cname year
drop if year>2020

*Get rid of unused variables
keep ccode cname year ccode_qog cname_qog ccodealp ccodecow cname_year ccodealp_year atop_number wdi_area wdi_areabelow wdi_gdpcapcon2015 wdi_gdppppcur undp_hdi cpds_lg cpds_ll cpds_ls cpds_vg cpds_vl cpds_vs ross_gas_exp ross_gas_prod ross_gas_value_2014 ross_oil_exp ross_oil_prod ross_oil_value_2014 wdi_pop wdi_popbelow wdi_popden wdi_enerenew lld_capacity

*Save second round of QoG Standard
save "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/QoG_Standard_Round2.dta"

**Merge the QoG_Standard_Round 2 and WithDecay&5Years datasets
use "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/WithDecay&5yrs.dta"
drop _merge
drop if missing(cname)
merge 1:1 ccodecow year using "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/qog_std_ts_jan23_stata14.2.dta"

**MERGE OF NEW QOG STANDARD VARIABLES TO MAIN DATASET COMPLETE**
save "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/FINALDATASET.dta"

drop if cname=="Georgia" &year<1992
drop if cname=="Azerbaijan" &year<1992
drop if cname=="Armenia" &year<1992
drop if cname=="Ukraine" &year<1992
drop if cname=="Belarus" &year<1992
drop if cname=="Lithuania" &year<1990
drop if cname=="Latvia" &year<1990
drop if cname=="Estonia" &year<1990
drop if cname=="Turkmenistan" &year<1992
drop if cname=="Tajikistan" &year<1992
drop if cname=="Kyrgyzstan" &year<1992
drop if cname=="Uzbekistan" &year<1992
drop if cname=="Kazakhstan" &year<1992

save "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/FINALDATASET.dta"
*Make variable that is total of social democrats, greens, and liberals in parliament by country by year
egen total = rowtotal(cpds_lg cpds_ls)
rename total socdem_green_coalit

*Make variable that is total of social democrats and greens in parliament by country by year
egen total = rowtotal(cpds_lg cpds_ll cpds_ls)
rename total leftcoalit

**Download Freedom House Index (2023)'s "Country and Territory Ratings and Statuses, 1973-2023" dataset on excel. Then manually convert to country-year format and add a variable that is the average of "CL" and "PR" variables. Then merge variable into main STATA file. 

**Download variables representing CO2 emissions per capita (in metric tons) and renewable energy consumption (as a percent of total energy consumption) from World Bank (2022)'s World Development Indicators DataBank on excel. Then manually convert to country-year format and merge variable into main STATA file.

**Download variables representing CO2 emissions per capita (in metric tons) and renewable energy consumption (as a percent of total energy consumption) from World Bank (2022)'s World Development Indicators DataBank on excel. Then manually convert to country-year format and merge variable into main STATA file.

** Export main STATA dataset as excel file. Then manually fill in values for the "wdi_areabelow" variable for intervening years (between 1990, 2000, and 2010, when data is already logged for this variable) for all states of which total land area did not change.

*Run negative binomial regression to test
xtset ccodecow year
nbreg ccl_lpp L.iead_rat ave_gov_enviro gov_coalit_seatshare FreeHouseInvert co2_emissions renewable_consumption wdi_gdpcapcon2015 emdat_damage undp_hdi wdi_pop wdi_areabelow, cl(ccodecow)

*Small Vars Coefplot
coefplot, drop(_cons ave_gov_enviro undp_hdi) xline(0)

*Large Vars Coefplot
coefplot, drop(_cons L.iead_rat gov_coalit_seatshare FreeHouseInvert co2_emissions renewable_consumption wdi_gdpcapcon2015 emdat_damage wdi_pop wdi_areabelow) xline(0)

*Marginsplots, margins analysis
margins, at(L.iead_rat=(0(1)24) _all(mean))
margins, at(ave_gov_enviro=(0(.05)1) _all(mean))
marginsplot

*Other plots, regressions generated as needed 

*Save and use
save "/Users/carterahanson/Downloads/Capstone Stata/Redoing for Do File/FINALDATASET6.dta"
use "/Users/carterahanson/Downloads/Capstone Stata/FINALDATASET6.dta", clear

drop if year<1970

save "/Users/carterahanson/Downloads/Capstone Stata/SummerRestart/SummerDataset1.dta"
file /Users/carterahanson/Downloads/Capstone Stata/SummerRestart/SummerDataset1.dta saved

drop act_act
drop bti_envc

drop if year<2012 &cname=="South Sudan"

drop if year<1991 &cname=="Estonia"
drop if year<1991 &cname=="Latvia"
drop if year<1991 &cname=="Lithuania"

save "/Users/carterahanson/Downloads/Capstone Stata/SummerRestart/SummerDataset1.dta", replace file /Users/carterahanson/Downloads/Capstone Stata/SummerRestart/SummerDataset1.dta saved

*Export new variables from World Bank (2022) to Excel, where I then went through and deleted rows to match STATA database's country-year.

	**For countries not included in World Bank (2022) dataset, I combined country-years to 	amalgamate USSR, Yugoslavia, Serbia & Montenegro
		***"control of corruption" variable was averaged, weighted by constituent country population
		***same for "employment in agriculture" and employment in industry", under the assumption that constituent countries provide portions of the labor market proportional to their populations (which is not necessarily true, but the best I came up with)	
		
		
import excel "/Users/carterahanson/Downloads/Capstone Stata/SummerRestart/P_Data_Extract_From_World_Development_Indicators 3.xlsx", sheet("Data") firstrow clear

rename Time year
drop if year>2020
rename CountryName cname
replace cname = "Kyrgyzstan" if cname=="Kyrgyz Republic"

save "/Users/carterahanson/Downloads/Capstone Stata/SummerRestart/NewControls.dta"
file /Users/carterahanson/Downloads/Capstone Stata/SummerRestart/NewControls.dta saved

*Add new control variables to main dataset
use "/Users/carterahanson/Downloads/Capstone Stata/SummerRestart/SummerDataset1.dta"
drop _merge

save "/Users/carterahanson/Downloads/Capstone Stata/SummerRestart/SummerDataset1.dta", replace
file /Users/carterahanson/Downloads/CapstoneStata/SummerRestart/SummerDataset1.dta saved

merge 1:1 cname year using "/Users/carterahanson/Downloads/Capstone Stata/SummerRestart/NewControls.dta"

save "/Users/carterahanson/Downloads/Capstone Stata/SummerRestart/SummerDataset2.dta"
file /Users/carterahanson/Downloads/CapstoneStata/SummerRestart/SummerDataset2.dta saved

*Make variable that is total of oil, gas, coal, and natural gas rents by country by year
egen total = rowtotal(natgas_rents coal_rents gas_rents oil_rents)
rename total fossil_rents
**Then manually sorted and deleted fossil_rents observations when any of natgas_rents coal_rents gas_rents oil_rents were missing data
order cname year ccodecow fossil_rents natgas_rents coal_rents gas_rents oil_rents
sort natgas_rents

replace fossil_rents =. if missing(natgas_rents)
replace fossil_rents =. if missing(coal_rents)
replace fossil_rents =. if missing(gas_rents)
replace fossil_rents =. if missing(oil_rents)

*Run some tests
xtset ccodecow year
nbreg ccl_lpp L.iead_rat ave_gov_enviro gov_coalit_seatshare FreeHouseInvert co2_tonspc industry_employ renewable_consumption gdppc2023 undp_hdi pop wdi_areabelow atop_number, cl(ccodecow)

corr ccl_lpp L.iead_rat ave_gov_enviro gov_coalit_seatshare FreeHouseInvert co2_tonspc industry_employ renewable_consumption gdppc2023 undp_hdi pop wdi_areabelow atop_number

save "/Users/carterahanson/Downloads/Capstone Stata/SummerRestart/SummerDataset2.dta", replace file /Users/carterahanson/Downloads/Capstone Stata/SummerRestart/SummerDataset2.dta 

*Exported to Excel and manually went through and corrected all country-year outputs for ave_gov_enviro, such that it is now weighted by party seatshare with the governing coalition, and gov_coalit_seatshare so it now better reflects reality of elections since 1970
**Then reuploaded now-corrected Excel spreadsheet to STATA
import excel "/Users/carterahanson/Downloads/Capstone Stata/SummerRestart/GroundUp4.xlsx", sheet("Sheet1") 
> firstrow clear

save "/Users/carterahanson/Downloads/Capstone Stata/SummerRestart/SummerDataset3.dta"
file /Users/carterahanson/Downloads/Capstone Stata/SummerRestart/SummerDataset3.dta saved

*Import new dataset for IEAs, which was generated from a manual review of the treaties included in the International Environmental Agreement Data Base
save "/Users/carterahanson/Downloads/Capstone Stata/SummerRestart/SummerDataset7.dta"
file /Users/carterahanson/Downloads/Capstone Stata/SummerRestart/SummerDataset7.dta saved

*Run tests
xtset ccodecow year
nbreg ccl_leglp L.rat_2 ave_gov_enviro gov_coalit_seatshare p_polity2 co2_tonspc industry_employ renewable_consumption gdppc2023 undp_hdi pop wdi_areabelow atop_number, cl(ccodecow)

xtset ccodecow year
nbreg ccl_lpp L.rat_2 ave_gov_enviro gov_coalit_seatshare p_polity2 co2_tonspc industry_employ renew_consum gdppc2023 undp_hdi pop wdi_areabelow atop_number, cl(ccodecow)

margins, at(ave_gov_enviro=(0(.02)1) _all(mean))
marginsplot

xtset ccodecow year
nbreg ccl_lpp L.rat_2 ave_gov_enviro gov_coalit_seatshare p_polity2 co2_tonspc industry_employ renew_consum gdppc2023 undp_hdi pop wdi_areabelow atop_number if year>1991&year<2019, cl(ccodecow)

*Make variables for robustness checks
order lag1, after (rat_1)

gen lag2 = l.rat_2
gen lead2 = f.rat_2
gen yrsum3_rat_2 = (rat_2+lead2+lag2)

gen yrsum3_sig_1_3 = (sig_1_3+ F.sig_1_3 +F2.sig_1_3)
gen yrsum3_rat_1_3 = (rat_1_3+ F.rat_1_3 +L.rat_1_3)


gen lead1 = f.rat_1_3
gen lead2 = f2.rat_1_3
gen yrsum3_rat_1_3 = (rat_1_3+lead1+lead2)


*Run Tests
xtset ccodecow year
nbreg ccl_leglp F.rat_1_3 ave_gov_enviro p_polity2 edgar_co2pc fossil_rents wdi_enerenew gdppc2023 life_expec pop wdi_areabelow atop_number if year>1990&year<2019, cl(ccodecow)

save "/Users/carterahanson/Downloads/Capstone Stata/SummerRestart/SummerDataset7.dta", replace


**Fill in some missing edgar_co2pc data and then reimport
import excel "/Users/carterahanson/Downloads/Capstone Stata/Jan24IEAedits.xls", sheet("Sheet1") firstrow clear

save "/Users/carterahanson/Downloads/Capstone Stata/SummerDataset8.dta"


*Make new fossil_rents2 variable that is total of oil, coal, and natural gas rents by country by year
egen total = rowtotal(natgas_rents coal_rents oil_rents)
rename total fossil_rents2
**Then manually sorted and deleted fossil_rents observations when any of natgas_rents coal_rents gas_rents oil_rents were missing data
order cname year ccodecow fossil_rents2 natgas_rents coal_rents gas_rents
sort natgas_rents

replace fossil_rents2 =. if missing(natgas_rents)
replace fossil_rents2 =. if missing(coal_rents)
replace fossil_rents2 =. if missing(oil_rents)


* Model 1
xtset ccodecow year
nbreg ccl_lpp rat_1_3 ave_gov_enviro p_polity2 edgar_co2pc fossil_rents2 wdi_enerenew gdppc2023 life_expec pop wdi_areabelow atop_number if year>1990&year<2019, cl(ccodecow)

* Model 2
xtset ccodecow year
nbreg ccl_lpp F.rat_1_3 ave_gov_enviro p_polity2 edgar_co2pc fossil_rents2 wdi_enerenew gdppc2023 life_expec pop wdi_areabelow atop_number if year>1990&year<2019, cl(ccodecow)

* Model 3
xtset ccodecow year
nbreg ccl_lpp F2.rat_1_3 ave_gov_enviro p_polity2 edgar_co2pc fossil_rents2 wdi_enerenew gdppc2023 life_expec pop wdi_areabelow atop_number if year>1990&year<2019, cl(ccodecow)

